﻿using System.Collections.Generic;
using System.Drawing;

namespace OpenShape
{
    class Labeler
    {
        public bool m_isLoad = false;

        public List<LabelObject> labels = new List<LabelObject>();
        
        public void CalculateLabelPoint(Layer ly)
        {
            float dx;
            float dy;

            switch (ly.m_nShapeType)
            {
                case 1: // point

                    for (int i = 0; i < ly.m_nRecords; i++)
                    {
                        GeoPoint m_SO = (GeoPoint)ly.m_SpatialObject[i];
                        LabelObject temp_label = new LabelObject();
                        temp_label.x = m_SO.Points[0].X;
                        temp_label.y = m_SO.Points[0].Y;
                        labels.Add(temp_label);
                    }

                    break;
                case 3://polyline
                    for (int i = 0; i < ly.m_nRecords; ++i)
                    {
                        GeoPolyline m_SO = (GeoPolyline)ly.m_SpatialObject[i];
                        dx = (float)(m_SO.MBR.xmax + m_SO.MBR.xmin) / 2;
                        dy = (float)(m_SO.MBR.ymax + m_SO.MBR.ymin) / 2;
                        LabelObject temp_label = new LabelObject();
                        temp_label.x = dx;
                        temp_label.y = dy;
                        labels.Add(temp_label);
                    }
                    break;
                case 5:// polygon
                    for (int i = 0; i < ly.m_nRecords; ++i)
                    {
                        GeoPolygon m_SO = (GeoPolygon)ly.m_SpatialObject[i];
                        dx = (float)(m_SO.MBR.xmax + m_SO.MBR.xmin) / 2;
                        dy = (float)(m_SO.MBR.ymax + m_SO.MBR.ymin) / 2;
                        LabelObject temp_label = new LabelObject();
                        temp_label.x = dx;
                        temp_label.y = dy;
                        labels.Add(temp_label);
                    }
                    break;

                default:
                    break;
            }

        }

        public void draw(Graphics g, Scaler sc, Layer ly)
        {
            for (int i = 0; i < ly.m_DBF.pDataTable.Rows.Count; i++)
            {
                double sx, sy;
                sc.WorldToDevice(ly.m_label.labels[i].x, ly.m_label.labels[i].y, out sx, out sy);
                g.DrawString(ly.m_label.labels[i].Text, ly.fonts , new SolidBrush(ly.pFontColor), (float)sx, (float)sy);
            }
        }

        public void ReadField(Layer ly, string FieldName)
        {
            int col_Idx = -1;

            if (FieldName == null || FieldName=="")
            {
                for (int i = ly.m_DBF.pDataTable.Columns.Count - 1; i >= 0; i--)
                {
                    if (ly.m_DBF.pDataTable.Columns[i].DataType == System.Type.GetType("System.String"))
                    {
                        FieldName = ly.m_DBF.pDataTable.Columns[i].ColumnName;
                        col_Idx = i;
                    }
                }
            }
            if (FieldName.Contains ("[") || FieldName.Contains ("]") || FieldName.Contains ("&") || FieldName.Contains ("\"") )
            {
                AnalysisFieldName(ly, FieldName);

            }
            else
            {
                for (int i = 0; i < ly.m_DBF.pDataTable.Columns.Count; i++)
                {
                    if (FieldName == ly.m_DBF.pDataTable.Columns[i].ColumnName.ToString())
                    {
                        col_Idx = i;
                    }
                }
                for (int i = 0; i < ly.m_DBF.pDataTable.Rows.Count; i++)
                {
                    ly.m_label.labels[i].Text = ly.m_DBF.pDataTable.Rows[i][col_Idx].ToString();
                }
            }

        }

        private static void AnalysisFieldName(Layer ly, string FieldName)
        {
            string[] strArray = FieldName.Split('&');
            int[] idxArray = new int[strArray.Length];

            for (int i = 0; i < strArray.Length; i++)
            {
                int temp_Idx = -1;
                if (strArray[i].Contains("[") || strArray[i].Contains("]"))
                {
                    string temp_field;
                    temp_field = strArray[i].Replace("[", "");
                    temp_field = temp_field.Replace("]", "");
                    temp_field = temp_field.Replace(" ", "");

                    for (int j = 0; j < ly.m_DBF.pDataTable.Columns.Count; j++)
                    {
                        if (temp_field == ly.m_DBF.pDataTable.Columns[j].ColumnName.ToString())
                        {
                            temp_Idx = j;
                        }
                    }
                    idxArray[i] = temp_Idx;
                }
                if (strArray[i].Contains("\""))
                {
                    charF pCharF = new charF();

                    strArray[i] = pCharF.Right (strArray[i], strArray[i].Length - strArray[i].IndexOf("\""));
                    strArray[i] = pCharF.Left(strArray[i], strArray[i].Length - strArray[i].IndexOf("\""));

                    strArray[i] = strArray[i].Replace("\"", "");
                    idxArray[i] = temp_Idx;
                }
            }

            for (int i = 0; i < ly.m_DBF.pDataTable.Rows.Count; i++)
            {
                string temp_str="";

                for (int j = 0; j < idxArray.Length; j++)
                {
                    if (idxArray[j] != -1)
                    {
                        temp_str += ly.m_DBF.pDataTable.Rows[i][idxArray[j]].ToString();
                    }
                    else
                    {
                        temp_str += strArray[j];
                    }
                }

                ly.m_label.labels[i].Text= temp_str;

            }
        }

    }
}
